#include "TCPServer.hpp"
#include "http.hpp"


httpResponse Login(httpRequest &req)
{
    httpResponse resp;
    std::cout << "外部已经拿到了参数" << std::endl;
    req.GetRequestBody();
    std::cout << "#############################" << std::endl;
    resp.addCode(200, "OK");
    resp.addBodyText("<html><h1>result done<h1></html>");


    // 1. pipe
    // 2. dup2
    // 3. fork()
    // 4. exec 系列函数来处理 python, PHP 或者java写的程序

    return resp;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cerr << "Usage: " << argv[0] << " local-port" << std::endl;
        exit(0);
    }
    uint16_t port = std::stoi(argv[1]);

    httpServer hserver;
    hserver.insertService("/login", Login);
    std::unique_ptr<tcpServer> tsvr = \
        std::make_unique<tcpServer>\
        (std::bind(&httpServer::handleHttpRequest, &hserver, std::placeholders::_1), port);
    tsvr->loop();

    return 0;
}
